/**
 * @file user_led.c
 *
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
#include "user_include.h"

uint8_t m_led_status = 0;
uint8_t led_status_array[LED_CONF_MAX] = {0};
uint8_t relay_status_array[RELAY_CONF_MAX] = {0};

void led_init(void)
{
    //zero-initialize the config structure.
    gpio_config_t io_conf = {};
    //disable interrupt
    io_conf.intr_type = GPIO_INTR_DISABLE;
    //set as output mode
    io_conf.mode = GPIO_MODE_OUTPUT;
    //bit mask of the pins that you want to set,e.g.GPIO18/19
    io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
    //disable pull-down mode
    io_conf.pull_down_en = 0;
    //disable pull-up mode
    io_conf.pull_up_en = 0;
    //configure GPIO with the given settings
    gpio_config(&io_conf);

    gpio_set_level(GPIO_OUTPUT_IO_0, 0);

    gpio_set_level(GPIO_RELAY_IO_1, 0);
    gpio_set_level(GPIO_RELAY_IO_2, 0);
    gpio_set_level(GPIO_RELAY_IO_3, 0);

    m_led_status = 0;
    memset(led_status_array, 0, sizeof(led_status_array));
    memset(relay_status_array, 0, sizeof(relay_status_array));
}

void set_led_array_status(uint8_t index, uint8_t status)
{
    if (index < LED_CONF_MAX)
	    led_status_array[index] = status;
    else
	    led_status_array[0] = status;
}

uint8_t get_led_array_status(uint8_t index)
{
    if (index < LED_CONF_MAX)
	    return led_status_array[index];
    else
	    return led_status_array[0];
}

void set_relay_array_status(uint8_t index, uint8_t status)
{
    if (index < RELAY_CONF_MAX)
	    relay_status_array[index] = status;
    else
	    relay_status_array[0] = status;
}

uint8_t get_relay_array_status(uint8_t index)
{
    if (index < RELAY_CONF_MAX)
	    return relay_status_array[index];
    else
	    return relay_status_array[0];
}

void set_led_status(uint8_t status)
{
    m_led_status = status;
}

uint8_t get_led_status(void)
{
    return m_led_status;
}


void user_led_task(void *args)
{
    static int status = 0xff;
    static int relay_status[RELAY_CONF_MAX] = {0xff};
    memset(relay_status, 0xff, sizeof(relay_status));

    led_init();

    while(1) {
        vTaskDelay(500 / portTICK_PERIOD_MS);
        if (status != m_led_status) {
            status = m_led_status;
            gpio_set_level(GPIO_OUTPUT_IO_0, status);
            printf("%s status:%d\r\n",__func__, status);
        }

        for (uint8_t i = 0; i < RELAY_CONF_MAX; i++) {
            if (relay_status[i] != relay_status_array[i]) {
                relay_status[i] = relay_status_array[i];

                if (i == 0) {
                    gpio_set_level(GPIO_RELAY_IO_1, relay_status[i]);
                } else if (i == 1) {
                    gpio_set_level(GPIO_RELAY_IO_2, relay_status[i]);
                } else if (i == 2) {
                    gpio_set_level(GPIO_RELAY_IO_3, relay_status[i]);
                }
                printf("%s [%d] status:%d\r\n", __func__, i, relay_status[i]);
            }
        }
    }
}
